import { $t } from '@/locales';

/**
 * Transform record to option
 *
 * @example
 *   ```ts
 *   const record = {
 *     key1: 'label1',
 *     key2: 'label2'
 *   };
 *   const options = transformRecordToOption(record);
 *   // [
 *   //   { value: 'key1', label: 'label1' },
 *   //   { value: 'key2', label: 'label2' }
 *   // ]
 *   ```;
 *
 * @param record
 */
export function transformRecordToOption<T extends Record<string, string>>(record: T) {
  return Object.entries(record).map(([value, label]) => ({
    value,
    label
  })) as CommonType.Option<keyof T>[];
}

/**
 * Translate options
 *
 * @param options
 */
export function translateOptions(options: CommonType.Option<string>[]) {
  return options.map(option => ({
    ...option,
    label: $t(option.label as App.I18n.I18nKey)
  }));
}

/**
 * Map number to letter
 * @param number
 */
export function mapNumberToLetter(number: number): string {
  if (number >= 1 && number <= 26) {
    // Convert the number to its corresponding ASCII code for uppercase letters (A=65, B=66, ..., Z=90)
    const letterCode = 64 + number;
    return String.fromCharCode(letterCode);
  } else if (number > 26) {
    // Handle cases where the input number is greater than 26
    let result = "";
    let remainingNumber = number;
    while (remainingNumber > 0) {
      const currentLetterCode = (remainingNumber - 1) % 26 + 1;
      result = String.fromCharCode(64 + currentLetterCode) + result;
      remainingNumber = Math.floor((remainingNumber - 1) / 26);
    }
    return result;
  } else {
    return "Invalid input. Please provide a number greater than 0.";
  }
}
